/*
 * @Descripttion:
 * @Author: Cxy
 * @Date: 2022-12-29 14:18:49
 * @LastEditors: Cxy
 * @LastEditTime: 2025-10-25 19:13:49
 * @FilePath: \xy-admin\play\src\router\index.ts
 */
import { createRouter, createWebHistory } from 'vue-router'
import basicR from './basicR'
import { addRouterRO, assemblyRO, filterMenuRO, findComponentsRO, sortRO, splicingRO } from './utils'
import beforeEach from './eachR'
import type { RouteRecordRaw } from 'vue-router'
import type { RouteConfigInter, RouteImportInter } from './type'

const modules: RouteImportInter = import.meta.glob(['./model/*.ts'], {
  import: 'default',
  eager: true
})

/* 静态路由 */
const staticR: Array<RouteConfigInter> = Object.values(modules).flat()

/* 后台返回动静态路由处理 */
export const staticDynamicRO = (data: Array<RouteConfigInter> = []) => {
  const dynamicR = assemblyRO(splicingRO(sortRO([...staticR, ...findComponentsRO(data)])))
  addRouterRO(dynamicR)
  const [homeR, ...noLayoutR] = dynamicR
  return filterMenuRO(sortRO(homeR.children.concat(noLayoutR)))
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: basicR as Array<RouteRecordRaw>
})

beforeEach(router)

export default router
